local PATH,IP = ...

IP = IP or "127.0.0.1"

package.path = string.format("%s/client/?.lua;%s/lualib/?.lua;%s/skynet/lualib/?.lua", PATH, PATH,PATH)
package.cpath = string.format("%s/skynet/luaclib/?.so;%s/lsocket/?.so", PATH, PATH)

local message = require "simplemessage"


message.peer(IP, 5678)
message.connect()

local event = {}

message.bind(event)


function event.ping(args)
	local now = os.clock()
	print("ping", now - args.t)
end

function event.signin(args)
	print("signin",args.ret)
	if args.ret == 0 then
		message.request("ping",{t=os.clock()})	-- should error before login
		message.request("login",{userid = "alice"} )
	else
		-- signin failed, signup
		message.request("signup", { userid = "alice" })
	end
end

function event.signup(args)
	print("signup", args.ret)
	if args.ret == 0 then
		message.request("signin", { userid = "alice" })
	else
		error "Can't signup"
	end
end

function event.login(args)
	print("login", args.ret)
	if args.ret == 0 then

		message.request("ping",{t=os.clock()})
	else
		print("login failed", args.ret) 
	end
end

function event.push(args)
	print("server push", args.text)
end

message.request("signin", { userid = "alice" })

while true do
	message.update()
end
